<template xmlns:el-col="http://www.w3.org/1999/html">
  <div class="app-container">
    <!--    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>-->
    <div v-if="!open">
      <el-row :gutter="10" class="mb8">

        <el-col :span="1.5">
          <router-link :to="'/property/office/oldGroup'" class="link-type">
            <el-button icon="el-icon-back" size="mini">
              返 回
            </el-button>
          </router-link>
        </el-col>

        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-hasPermi="['module:attendanceGroup:add']">新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                     v-hasPermi="['module:attendanceGroup:edit']">修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                     v-hasPermi="['module:attendanceGroup:remove']">删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                     v-hasPermi="['module:attendanceGroup:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
        <!--      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="${comment}" align="center" prop="id" v-if="false"/>-->
        <el-table-column label="物业公司" align="center" prop="propertyName" />
        <el-table-column label="小区" align="center" prop="estateName" />

        <!--        <el-table-column label="使用范围" align="center" prop="groupGridLevel" width="120">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-tag v-if="scope.row.groupGridLevel === 2" style="cursor: pointer" :type="''">总部</el-tag>-->
        <!--            <el-tag v-if="scope.row.groupGridLevel === 1" style="cursor: pointer" :type="''">分部</el-tag>-->
        <!--          </template>-->
        <!--        </el-table-column>-->

        <el-table-column label="考勤组名" align="center" prop="groupName" />
        <!--        <el-table-column label="上午上班" align="center" prop="amBeginDutyTime"  width="150"/>
                <el-table-column label="上午下班" align="center" prop="amEndDutyTime"  width="150"/>
                <el-table-column label="下午上班" align="center" prop="pmBeginDutyTime"  width="150"/>
                <el-table-column label="下午下班" align="center" prop="pmEndDutyTime"  width="150"/>-->
        <el-table-column label="班次" align="center" prop="shiftName" />
        <!--      <el-table-column label="${comment}" align="center" prop="lng" />
              <el-table-column label="${comment}" align="center" prop="lat" />-->


        <!--        <el-table-column label="范围" align="center" prop="signRange" >-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.signRange + "米" }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->

        <!--      <el-table-column label="部门id" align="center" prop="deptId"/>-->
        <el-table-column label="是否为默认" align="center" prop="isDefault" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isDefault === 0" style="cursor: pointer" :type="''">非默认</el-tag>
            <el-tag v-if="scope.row.isDefault === 1" style="cursor: pointer" :type="''">默认</el-tag>
          </template>
        </el-table-column>

        <!--        <el-table-column label="工作制度" align="center" prop="workSystem" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-tag v-if="scope.row.workSystem === 0" style="cursor: pointer" :type="'danger'">全周无休</el-tag>-->
        <!--            <el-tag v-if="scope.row.workSystem === 1" style="cursor: pointer" :type="''">每周一休</el-tag>-->
        <!--            <el-tag v-if="scope.row.workSystem === 2" style="cursor: pointer" :type="'success'">每周双休</el-tag>-->
        <!--            <el-tag v-if="scope.row.workSystem === 3" style="cursor: pointer" :type="'warning'">其他</el-tag>-->
        <!--          </template>-->
        <!--        </el-table-column>-->

        <!--      <el-table-column label="工作日 星期1  0 - 否  1 - 是" align="center" prop="workDay1" />
              <el-table-column label="工作日 星期2  0 - 否  1 - 是" align="center" prop="workDay2" />
              <el-table-column label="工作日 星期3  0 - 否  1 - 是" align="center" prop="workDay3" />
              <el-table-column label="工作日 星期4  0 - 否  1 - 是" align="center" prop="workDay4" />
              <el-table-column label="工作日 星期5  0 - 否  1 - 是" align="center" prop="workDay5" />
              <el-table-column label="工作日 星期6  0 - 否  1 - 是" align="center" prop="workDay6" />
              <el-table-column label="工作日 星期7  0 - 否  1 - 是" align="center" prop="workDay7" />-->
        <!--        <el-table-column label="特殊考勤组" align="center" prop="isSpecial" width="150">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-tag v-if="scope.row.isSpecial === 0" style="cursor: pointer" :type="''">非特殊</el-tag>-->
        <!--            <el-tag v-if="scope.row.isSpecial === 1" style="cursor: pointer" :type="'success'">特殊</el-tag>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="450">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-user-solid">
              <router-link
                :to="'/property/office/attendance/group_user?groupId=' + scope.row.id+'&propertyId='+ queryParams.propertyId"
                class="link-type">
                考勤人员
              </router-link>
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-user-solid">
              <router-link
                :to="'/property/office/work?dutyGroupId=' + scope.row.id +'&propertyId='+ queryParams.propertyId"
                class="link-type">
                节假日设置
              </router-link>
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-user-solid">
              <router-link
                :to="'/property/office/attendanceList?dutyGroupId=' + scope.row.id +'&propertyId='+ queryParams.propertyId"
                class="link-type">
                请假、出勤
              </router-link>
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['module:attendanceGroup:edit']">修改
            </el-button>

            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['module:attendanceGroup:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>

    </div>
    <div v-if="open">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="14">

            <el-row>
              <el-col :span="10">
                <el-form-item label="考勤组名" prop="groupName">
                  <el-input v-model="form.groupName" placeholder="请输入考勤组名" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="考勤班次" prop="attendanceWorkShift " @change="getTimeInfo">
                  <el-select v-model="form.attendanceWorkShift" placeholder="请选择班次" style="width:100%;" size="mini"
                             filterable clearable>
                    <el-option v-for="shift in shiftList"
                               :key="shift.id"
                               :label="shift.shiftName"
                               :value="shift.id"/>
                  </el-select>
                </el-form-item>
              </el-col>


              <el-col :span="10">
                <el-form-item label="物业公司" prop="propertyId">
                  <el-select v-model="form.propertyId" placeholder="请选择物业" style="width:100%;" size="mini"
                             clearable>
                    <el-option v-for="property in propertyOptions"
                               :key="property.id"
                               :label="property.propertyName"
                               :value="property.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="所属小区" prop="estateId">
                  <el-select v-model="form.estateId" placeholder="请选择小区" @change="getSysUserList(form.estateId)" style="width:100%;" size="mini" filterable
                             clearable @focus="getTimeInfo">
                    <el-option v-for="estate in estateChildOptions"
                               :key="estate.id"
                               :label="estate.estateName"
                               :value="estate.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="this.form.attendanceWorkShift!=null">
              <!--                <el-col span="10"><span  size="10">* 选择班次后请同步时间</span></el-col>-->
              <el-col span="10">
                <el-button @click="getTimeInfo" size="mini" style="margin-left:80px">同步时间</el-button>
              </el-col>

            </el-row>
            <el-row style="margin-top:10px ">
              <el-col :span="10">
                <el-form-item label="上午上班" prop="amBeginDutyTime">
                  <el-input v-model="form.amBeginDutyTime" type="time" :disabled="form.amBeginDutyTime!=undefined" placeholder="上班时间" size="mini"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="10">
                <el-form-item label="提前打卡" prop="amBeginDutyHour">
                  <el-input v-model="form.amBeginDutyHour" type="number" :disabled="form.amBeginDutyHour!=undefined" placeholder="小时" size="mini">
                    <template slot="append">小时</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="10">
                <el-form-item label="上午下班" prop="amEndDutyTime">
                  <el-input v-model="form.amEndDutyTime" type="time" :disabled="form.amEndDutyTime!=undefined" placeholder="下班时间" size="mini"/>
                </el-form-item>
              </el-col>

              <el-col :span="10">
                <el-form-item label="延迟打卡" prop="amEndDutyHour">
                  <el-input v-model="form.amEndDutyHour" type="number" :disabled="form.amEndDutyHour!=undefined" placeholder="小时" size="mini">
                    <template slot="append">小时</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="10">
                <el-form-item label="下午上班" prop="pmBeginDutyTime">
                  <el-input v-model="form.pmBeginDutyTime" type="time" :disabled="form.pmBeginDutyTime!=undefined" placeholder="上班时间" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="提前打卡" prop="pmBeginDutyHour">
                  <el-input v-model="form.pmBeginDutyHour" type="number" :disabled="form.pmBeginDutyHour!=undefined" placeholder="小时" size="mini">
                    <template slot="append">小时</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>


            <el-row>
              <el-col :span="10">
                <el-form-item label="下午下班" prop="pmEndDutyTime">
                  <el-input v-model="form.pmEndDutyTime" type="time" :disabled="form.pmEndDutyTime!=undefined" placeholder="下班时间" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="延迟打卡" prop="pmEndDutyHour">
                  <el-input v-model="form.pmEndDutyHour" type="number" :disabled="form.pmEndDutyHour!=undefined" placeholder="小时" size="mini">
                    <template slot="append">小时</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="10">
                <el-form-item label="经纬度" prop="lng">
                  <el-input v-model="form.lng" placeholder="请输入经度" style="width:47%" size="mini"/>
                  <el-input v-model="form.lat" placeholder="请输入纬度" style="width:47%" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="考勤范围" prop="signRange">
                  <el-input v-model="form.signRange" placeholder="请输入范围 米" size="mini">
                    <template slot="append">米</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="20">
                <el-form-item label="考勤人员" prop="userIds">
                  <el-select v-model="form.userIds" placeholder="请选择考勤人员" style="width:100%;" size="mini" filterable multiple clearable>
                    <el-option v-for="estate in sysUserList"
                               :key="estate.userId"
                               :label="estate.nickName"
                               :value="estate.userId"/>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-col :span="12">
              <el-form-item label="是否默认" prop="isDefault">
                <el-radio-group v-model="form.isDefault" size="mini">
                  <el-radio :value="1" :label="1">默认</el-radio>
                  <el-radio :value="0" :label="0">非默认</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="10">
            <div style="width:100%;height:400px;">
              <TianDiTu :zoom="12" :label-title="form.groupName" :radius-circle="form.signRange"  :shape-type="'circle'"
                        :default-lat="parseFloat(form.lat)" :default-lng="parseFloat(form.lng)"
                        @pickLngLat="pickLngLat"></TianDiTu>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </div>
    <!-- 添加或修改考勤组对话框 -->

  </div>
</template>
<script>
import {listGroup, getGroup, delGroup, addGroup, updateGroup, exportGroup} from "@/api/module/attendance/group";
import {listEstate} from "@/api/module/estate/estate";
import {listProperty} from "@/api/module/property/property";
import {getShift, getShiftList, listShift} from "@/api/module/attendance/shift";
import {listUser} from "@/api/system/user";
import {exportAttendance} from "@/api/module/attendance/attendance";

export default {
  name: "Group",
  data() {
    return {
      map: null,
      marker: null,
      tipinput: '',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤组表格数据
      groupList: [],
      shiftList: [],
      sysUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      propertyOptions: [],

      estateChildOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        propertyId: undefined,
        sysUserName: undefined,
      },
      queryUsers: {
        propertyId: undefined,
        estateId: undefined,
      },
      /*      queryShiftList:{
              propertyId:undefined,
            },*/
      /*queryTime:{
        attendanceWorkShift:this.form.attendanceWorkShift,
      },*/
      // 表单参数
      form: {},
      rules: {
        groupName: [
          {required: true, message: "考勤组名称不能为空", trigger: "blur"}
        ],
        propertyId: [
          {required: true, message: "物业公司不能为空", trigger: "blur"}
        ],
        lng: [
          {required: true, message: "请设置地址", trigger: "blur"}
        ],
        lat: [
          {required: true, message: "请设置地址", trigger: "blur"}
        ],
        signRange: [
          {required: true, message: "请设置打卡范围", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.queryParams.propertyId = this.$route.query.propertyId ? parseInt(this.$route.query.propertyId) : undefined;
    this.queryParams.estateId = this.$route.query.estateId ? parseInt(this.$route.query.estateId) : undefined;
    this.getList();
    // this.getSysUserList();
  },
  methods: {
    /** 查询考勤组列表 */
    getList() {
      this.loading = true;
      this.queryParams.sysUserName = this.$store.state.user.name;
      listGroup(this.queryParams).then(response => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWorkShift() {
      listShift().then(response => {
        this.shiftList = response.rows;
      });
    },
    getTimeInfo() {
      if (this.form.attendanceWorkShift != undefined) {
        getShift(this.form.attendanceWorkShift).then(response => {
          this.form.amBeginDutyTime = response.data.amBeginDutyTime;
          this.form.amBeginDutyHour = response.data.amBeginDutyHour;
          this.form.amEndDutyTime = response.data.amEndDutyTime;
          this.form.amEndDutyHour = response.data.amEndDutyHour;
          this.form.pmBeginDutyTime = response.data.pmBeginDutyTime;
          this.form.pmBeginDutyHour = response.data.pmBeginDutyHour;
          this.form.pmEndDutyTime = response.data.pmEndDutyTime;
          this.form.pmEndDutyHour = response.data.pmEndDutyHour;
        })
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        propertyId: undefined,
        estateId: undefined,
        groupName: undefined,
        amBeginDutyTime: undefined,
        amEndDutyTime: undefined,
        pmBeginDutyTime: undefined,
        pmEndDutyTime: undefined,
        amBeginDutyHour: undefined,
        amEndDutyHour: undefined,
        pmBeginDutyHour: undefined,
        pmEndDutyHour: undefined,
        lng: undefined,
        lat: undefined,
        signRange: undefined,
        isDefault: undefined,
        workDay1: undefined,
        workDay2: undefined,
        workDay3: undefined,
        workDay4: undefined,
        workDay5: undefined,
        workDay6: undefined,
        workDay7: undefined,
        isSpecial: undefined,
        attendanceWorkShift: undefined,
        userIds: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length
    },
    getSysUserList(estateId) {
      this.queryUsers.propertyId = this.form.propertyId;
      this.queryUsers.estateId = estateId;
      console.log(estateId);
      listUser(this.queryUsers).then(response => {
        console.log(response);
        this.sysUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    pickLngLat(e){
      console.log('getLngLat e === ', e)
      this.form.lng = e.lng.toFixed(6);
      this.form.lat = e.lat.toFixed(6);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getPropertyChildOptionlist();
      this.getEstateChildOptionlist();
      this.getWorkShift();
      var that = this;
      this.reset();
      this.open = true;
      this.title = "添加考勤组";
    },
    getEstateChildOptionlist() {
      listEstate().then(response => {
        this.estateChildOptions = response.rows;
      });
    },

    getPropertyChildOptionlist() {
      listProperty().then(response => {
        this.propertyOptions = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.getSysUserList(row.estateId)
      this.getPropertyChildOptionlist();
      this.getEstateChildOptionlist();
      this.getWorkShift();
      this.reset();
      var that = this;
      const id = row.id || this.ids;
      getGroup(id).then(response => {
        console.log(response);
        this.loading = false;
        this.form = response.data;
        // this.getSysUserList();
        this.open = true;
        this.title = "修改考勤组";
      });
    },
    handleExport() {
      const queryParams = this.queryParams;
      queryParams.propertyId = this.$route.query.propertyId;
      this.$confirm('是否确认导出公司所有考勤数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportAttendance(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除现金奖励规则编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delGroup(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};

</script>

<style lang="scss" scoped>
.mapSelect {
  position: absolute;
  top: 0px;
  right: 60px;
  background: #fff none repeat scroll 0 0;
  border: 1px solid #ccc;
  margin: 10px auto;
  padding: 6px;
  font-family: "Microsoft Yahei", "微软雅黑", "Pinghei";
  font-size: 14px;
}
</style>

<style lang="scss">
.amap-sug-result {
  z-index: 99999 !important;
}
</style>
